<?php

/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#namespace tehframework\iocc\bindings;

#use \ReflectionClass;
#use \ReflectionMethod;
#use \ReflectionParameter;
#use tehframework\iocc\valueholders\PrimitiveValueHolder;
#use tehframework\iocc\valueholders\ReferenceValueHolder;

class AutowiredBinding extends ConfigurableBinding
{
	/**
	 * @var bool
	 */
	private $autowired;
	
	/**
	 * @param  string|array<string> $types
	 * @param  string? $name
	 * @param  string? $scope
	 * @param  callable? $provider
	 * @throws InvalidArgumentException
	 */
	public function __construct($types, $name = null, $scope = null, $provider = null)
	{
		parent::__construct($types, $name, $scope);
	}
	
	/**
	 * @return ObjectDefinition
	 */
	public function getDefinition()
	{
		$definition = parent::getDefinition();
		
		if (!$this->autowired)
		{
			$this->autowired = true;
			$this->autowire();
		}
		
		return $definition;
	}
	
	protected function autowire()
	{
		$types = $this->getTypes();
		
		$class = new ReflectionClass($types[0]);
		$ctor  = $class->getConstructor();
		
		if ($ctor)
		{
			$this->autowireMethod($ctor);
		}
	}
	
	/**
	 * @param  ReflectionMethod $method
	 */
	protected function autowireMethod(ReflectionMethod $method)
	{
		$arguments = array();
		
		if ($method->getNumberOfRequiredParameters() !== 0)
		{
			$arguments = $this->autowireArguments($method->getParameters());
		}
		
		$this->__call($method->getName(), $arguments);
	}
	
	/**
	 * @param  array<ReflectionParameter> $parameters
	 * @return array<int,ValueHolder>
	 */
	protected function autowireArguments(array $parameters)
	{
		$arguments = array();
		
		/* @var $parameter ReflectionParameter */
		foreach ($parameters as $parameter)
		{
			$arguments[$parameter->getPosition()] = $this->autowireArgument($parameter);
		}
		
		return $arguments;
	}
	
	/**
	 * @param  ReflectionParameter $parameter
	 * @return ValueHolder
	 */
	protected function autowireArgument(ReflectionParameter $parameter)
	{
		if ($parameter->isDefaultValueAvailable())
		{
			return new PrimitiveValueHolder($parameter->getDefaultValue());
		}
		
		/* @var $class ReflectionClass */
		$class = $parameter->getClass();
		
		if (!$class)
		{
			throw new ObjectBuildingException(sprintf(
				'Cannot autowire non-type-hinted [%s] argument of [%s::%s] method.',
				$parameter->getName(),
				$parameter->getDeclaringClass()->getName(),
				$parameter->getDeclaringFunction()->getName()
			));
		}
		
		return new ReferenceValueHolder($class->getName());
	}
}